package pers.baiban.sqltoword.convert;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import pers.baiban.sqltoword.enums.AllowEmpty;
import pers.baiban.sqltoword.model.Columns;
import pers.baiban.sqltoword.vo.ColumnVO;

import java.util.Objects;

/**
 * @author 强子初
 * @since 2023/11/27
 */
@Component
public class ColumnsToColumnVOConverter implements Converter<Columns, ColumnVO> {

    @Override
    public ColumnVO convert(Columns source) {
        ColumnVO target = BeanUtil.copyProperties(source, ColumnVO.class);
        target.setNumber(Math.toIntExact(source.getOrdinalPosition()));
        target.setColumnKey(new StringToTableIndexConverter().convert(source.getColumnKey()).getDesc());
        target.setIsNullable(new StringToAllowEmptyConverter().convert(source.getIsNullable()).getDesc());
        target.setNotNullable((Objects.equals(source.getIsNullable(), "YES") ? AllowEmpty.NO : AllowEmpty.YES).getDesc());
        target.setIsPrimaryKey((Objects.equals(source.getColumnKey(), "PRI") ? AllowEmpty.YES : AllowEmpty.NO).getDesc());
        target.setColumnType(source.getColumnType().toUpperCase());
        return target;
    }
}